feat: add instrumented go-cache wrapper with Prometheus metrics - #214
Closed
DerekFrank wants to merge 1 commit into
Closed
feat: add instrumented go-cache wrapper with Prometheus metrics#214DerekFrank wants to merge 1 commit into
DerekFrank wants to merge 1 commit into
Conversation
Add operatorpkg/cache, a near drop-in wrapper around patrickmn/go-cache
that emits Prometheus metrics under the operator_cache_* subsystem:
- gets_total{name,result=hit|miss}
- adds_total{name,result=added|exists} (exists = suppressed duplicate)
- evictions_total{name} (TTL expiry)
- deletes_total{name,type=explicit|flush}
- flushes_total{name}
- flush_size{name} (histogram, entries per flush)
- entries{name} (gauge)
The wrapper embeds *cache.Cache so every underlying method still works;
only Get/Set/SetDefault/Add/Delete/Flush are overridden. Counter/gauge
series are pre-initialized to 0 to avoid sparse series. The entries gauge
is updated on mutation (never at scrape time) so scrapes do no cache work.
A caller-supplied OnEvicted callback is chained, not replaced.
ryan-mist
reviewed
Aug 12, 2026
| // Get records a hit or miss and delegates to the underlying cache. | ||
| func (c *Cache) Get(k string) (interface{}, bool) { | ||
| v, ok := c.Cache.Get(k) | ||
| if ok { |
Member
There was a problem hiding this comment.
Contributor
Author
There was a problem hiding this comment.
Yea I think we can drop it
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Add operatorpkg/cache, a near drop-in wrapper around patrickmn/go-cache that emits Prometheus metrics under the operator_cache_* subsystem:
The wrapper embeds *cache.Cache so every underlying method still works; only Get/Set/SetDefault/Add/Delete/Flush are overridden. Counter/gauge series are pre-initialized to 0 to avoid sparse series. The entries gauge is updated on mutation (never at scrape time) so scrapes do no cache work. A caller-supplied OnEvicted callback is chained, not replaced.
Issue #, if available:
Description of changes:
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.